package com.dormitory.ui;

import com.dormitory.model.Building;

import javax.swing.*;
import java.awt.*;

public class BuildingFormDialog extends JDialog {
    private JTextField nameField;
    private JTextField floorsField;
    private JComboBox<String> typeComboBox;
    private boolean confirmed = false;
    private Building building;

    public BuildingFormDialog(Window owner, Building building) {
        super(owner, "楼栋信息", ModalityType.APPLICATION_MODAL);
        this.building = building;
        initUI();
        if (building != null) {
            fillForm(building);
        }
        pack();
        setLocationRelativeTo(owner);
    }

    private void initUI() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 楼栋名称
        gbc.gridx = 0; gbc.gridy = 0;
        panel.add(new JLabel("楼栋名称："), gbc);
        gbc.gridx = 1;
        nameField = new JTextField(15);
        panel.add(nameField, gbc);

        // 总层数
        gbc.gridx = 0; gbc.gridy = 1;
        panel.add(new JLabel("总层数："), gbc);
        gbc.gridx = 1;
        floorsField = new JTextField(15);
        panel.add(floorsField, gbc);

        // 楼栋类型
        gbc.gridx = 0; gbc.gridy = 2;
        panel.add(new JLabel("楼栋类型："), gbc);
        gbc.gridx = 1;
        typeComboBox = new JComboBox<>(new String[]{"男生宿舍", "女生宿舍", "混合宿舍"});
        panel.add(typeComboBox, gbc);

        // 按钮
        JPanel btnPanel = new JPanel();
        JButton okBtn = new JButton("确定");
        JButton cancelBtn = new JButton("取消");
        okBtn.addActionListener(e -> onOK());
        cancelBtn.addActionListener(e -> onCancel());
        btnPanel.add(okBtn);
        btnPanel.add(cancelBtn);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(btnPanel, BorderLayout.SOUTH);
    }

    private void fillForm(Building b) {
        nameField.setText(b.getBuildingName());
        floorsField.setText(String.valueOf(b.getTotalFloors()));
        typeComboBox.setSelectedItem(b.getBuildingType());
    }

    private void onOK() {
        if (nameField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "楼栋名称不能为空！");
            return;
        }
        int totalFloors;
        try {
            totalFloors = Integer.parseInt(floorsField.getText().trim());
            if (totalFloors < 1 || totalFloors > 100) {
                JOptionPane.showMessageDialog(this, "总层数应在1-100之间！");
                return;
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "总层数必须为数字！");
            return;
        }
        confirmed = true;
        if (building == null) building = new Building();
        building.setBuildingName(nameField.getText().trim());
        building.setTotalFloors(totalFloors);
        building.setBuildingType((String) typeComboBox.getSelectedItem());
        dispose();
    }

    private void onCancel() {
        confirmed = false;
        dispose();
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public Building getBuilding() {
        return building;
    }
} 